﻿using System;
using System.Collections.Generic;
using System.Web;
using SubSonic;
using CMSDAL;
using CompanyCMS.App_Code;
using System.Security.Cryptography;

namespace CompanyCMS.App_Code
{
    public class CommonBLL
    {
        public static bool Login(string name, string pwd)
        {
            bool flag = false;
            pwd = Md5Encrpty(pwd);
            try
            {
                SqlQuery query = new Select().From("Administratortb")
                    .Where(Administratortb.Columns.AdminId).IsEqualTo(name)
                    .And(Administratortb.Columns.Password).IsEqualTo(pwd);
                AdministratortbCollection ac = query.ExecuteAsCollection<AdministratortbCollection>();
                if (ac != null && ac.Count == 1)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                LogHelper.Instance.WriteLog(e.Message + e.StackTrace);
            }
            return flag;
        }

        private static string Md5Encrpty(string message)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(message, "MD5");
        }

        public static AdministratortbCollection GetUers()
        {
            SqlQuery query = new Select().From(Administratortb.Schema);
            return query.ExecuteAsCollection<AdministratortbCollection>();
        }

        public static bool RegUser(string name, string pwd,out string errormessage)
        {
            errormessage = "";
            bool flag = false;
            try
            {
                SqlQuery query = new Select().From("Administratortb")
                    .Where(Administratortb.Columns.AdminId).IsEqualTo(name);
                if (query.GetRecordCount() == 0)
                {
                    Administratortb atb = new Administratortb();
                    atb.AdminId = name;
                    atb.Password = Md5Encrpty(pwd);
                    atb.Save();
                    flag = true;
                }
                else
                {
                    errormessage = "该用户已存在"; 
                }
            }
            catch (Exception e)
            {
                errormessage = e.Message;
            }
            return flag;
        }

        public static void DeleteUser(string name)
        {
            SqlQuery q = new Delete().From("Administratortb")
                .Where(Administratortb.Columns.AdminId).IsEqualTo(name);
            q.Execute();
        }

        public static BrandtbCollection GetBrand(int pageindex)
        {
            SqlQuery query = new Select().From("Brandtb");
            query.Paged(pageindex, 15);
            return query.ExecuteAsCollection<BrandtbCollection>();
            
        }

        public static bool AddBrand(string name, string detail, int order)
        {
            bool flag = false;
            try
            {
                Brandtb btb = new Brandtb();
                btb.BrandName = name;
                btb.BrandDetails = detail;
                btb.BrandOrder = order;
                btb.Save();
                flag = true;
            }
            catch (Exception e)
            {
                LogHelper.Instance.WriteLog(e.Message + e.StackTrace);
            }
            return flag;
        }

        public static CategorytbCollection GetCategories(int pageindex)
        {
            SqlQuery query = new Select().From("Categorytb");
            query.Paged(pageindex, 15);
            return query.ExecuteAsCollection<CategorytbCollection>();
        }

        public static ProducttbCollection GetProducts(int pageindex)
        {
            SqlQuery query = new Select().From("Producttb");
            query.Paged(pageindex, 15);
            return query.ExecuteAsCollection<ProducttbCollection>();
        }

        public static bool AddCategory(string name, int brandid, int order)
        {
            bool flag = false;
            try
            {
                Categorytb ctb = new Categorytb();
                ctb.BrandId = brandid;
                ctb.CategoryName = name;
                ctb.CategoryOrder = order;
                ctb.Save();
                flag = true;
            }
            catch (Exception e)
            {
                LogHelper.Instance.WriteLog(e.Message + e.StackTrace);
            }
            return flag;
        }

        

        //public static 
    }
}